<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../layui/css/layui.css" type="text/css">
    <style>
        body{
            width: 400px;
            height: 300px;
        }
    </style>
</head>
<body>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">登录名</label>
            <div class="layui-input-block">
                <input type="text" name="login_name" required  lay-verify="required" placeholder="请输入登录名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">原密码</label>
            <div class="layui-input-inline">
                <input type="password" name="old_password" required lay-verify="required" placeholder="请输入原密码" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-inline">
                <input type="password" name="new_password" required lay-verify="required" placeholder="请输入新密码" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>


<script src="../layui/layui.js"></script>
<script src="../js/ajax.js"></script>
<script>
    layui.use(['form', 'layer'], function(){
        var form = layui.form,
            layer = layui.layer;
        form.on('submit(formDemo)', function(data){
            //console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            //console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            //console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            if(data.field.old_password == data.field.new_password){
                layer.alert('新密码不能与旧密码一样!');
                return false;
            }
 
            // 添加加载动画
            var index2 = layer.load(2, {time: 10*1000});
            ajax({
                url: './php/index_user_resPwd.php',
                data: data.field,
                method: 'post',
                success: function(res){
                    //关闭
                    layer.close(index2);     
                    //console.log(res)
                    if(res == 1){
                        layer.alert('修改成功', function(index){
                            top.location.href = '../login.html';
                            layer.close(index);
                        });
                    }else if(res == 0){
                        layer.alert('修改失败, 登录名错误');
                    }else if(res == 2){
                        layer.alert('修改失败, 原密码错误');
                    }else {
                        layer.alert('修改失败, 请联系相关人员!');
                    }
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        
    });
</script>
</body>
</html>